# @Time : 2021/5/13 14:22
# @Author : Fioman 
# @Phone : 13149920693
import time

from vision_process.image_operation import *
from tools.fioman_math_tools import *

"""
铡刀相机拍照的图片的额标定工具,主要用来识别铡刀那里的图片信息
"""

# 参数设置,寻找轮廓的使用,使用的像素阈值以及过滤掉的轮廓面积
thresUsed = 150
areaFilter = 300


def cutter_image_show(name, image, showState=False):
    if showState:
        sizeScale = 3
        newWidth, newHeight = image.shape[1] // sizeScale, image.shape[0] // sizeScale
        showImage = cv.resize(image, (newWidth, newHeight), interpolation=cv.INTER_AREA)
        cv.namedWindow("{}".format(name))
        cv.imshow("{}".format(name), showImage)
        cv.waitKey(0)


def get_first_thres_point_final_check_cam_2(image, lightPos3, lightPos4):
    """
    相机2终点检测的时候调用的逻辑,唯一的不同点,这里找的是右边的轮廓.
    :param image: 传过来的原图
    :param lightPos3: 灯管3的位置信息
    :param lightPos4: 灯管4的位置信息
    :return:
    """
    xStart, xEnd, yStart, yEnd = lightPos3
    if xStart < xEnd:
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd, xStart:xEnd:-1]
    imageColor = cv.cvtColor(image, cv.COLOR_GRAY2BGR)
    cutter_image_show("Ruler3", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate3", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres3", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀2号相机3号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]
    if len(rightContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in rightContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("RightContourJointed3", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀1号相机终点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]

    if len(rightContours) >= 1:
        finalContour = sorted(rightContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[0], rightClockWisePoint[3]]  # 右边轮廓的左边线
        firstThresPointX3 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint3", imageColor)

    else:
        cv.line(imageColor, (xEnd, yStart), (xEnd, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint3", imageColor)
        if xStart < xEnd:
            firstThresPointX3 = xEnd - xStart
        else:
            firstThresPointX3 = xStart - xEnd

    xStart, xEnd, yStart, yEnd = lightPos4
    if xStart < xEnd:
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd, xStart:xEnd:-1]
    cv.destroyAllWindows()
    cutter_image_show("Ruler4", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate4", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres4", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀2号相机4号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]
    if len(rightContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in rightContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("RightContourJointed4", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀2号相机4号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]

    if len(rightContours) >= 1:
        finalContour = sorted(rightContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[0], rightClockWisePoint[3]]  # 右边轮廓的左边线
        firstThresPointX4 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint4", imageColor)

    else:
        cv.line(imageColor, (xEnd, yStart), (xEnd, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint4", imageColor)
        # 右边的轮廓没有找到,这个时候,要做的事情是什么呢,如果轮廓没有找到,那就证明整个灯管都被搞到了
        if xStart < xEnd:
            firstThresPointX4 = xEnd - xStart
        else:
            firstThresPointX4 = xStart - xEnd
    return int(firstThresPointX3), int(firstThresPointX4), imageColor


def get_first_thres_point_final_check_cam_1(image, lightPos1, lightPos2):
    """
    相机一终检的时候,调用的逻辑.注意要找到的轮廓图,左边是黑色的,所以找到的轮廓是左边的轮廓,如果这个时候也找到了右边的轮廓,就证明有漏光,就不考虑这种情况,
    如果左边的轮廓也没有找到,那就证明灯管被全部遮挡了,一般这种可能性也不大
    :param image: 拍摄的图片
    :param lightPos1: 1号灯管的位置
    :param lightPos2: 2号灯管的位置
    :return: 找到的点在灯管的位置,还有就是彩色图片,标定了找到的位置的彩色图片.
    """
    xStart, xEnd, yStart, yEnd = lightPos1
    if xStart < xEnd:
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd, xStart:xEnd:-1]
    imageColor = cv.cvtColor(image, cv.COLOR_GRAY2BGR)
    cutter_image_show("Ruler1", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate1", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres1", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀1号相机1号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]
    if len(rightContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in rightContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("RightContourJointed1", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀1号相机终点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]

    if len(rightContours) >= 1:
        finalContour = sorted(rightContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[0], rightClockWisePoint[3]]  # 右边轮廓的左边线
        firstThresPointX1 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint1", imageColor)

    else:
        cv.line(imageColor, (xEnd, yStart), (xEnd, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint1", imageColor)
        if xStart < xEnd:
            firstThresPointX1 = xEnd - xStart
        else:
            firstThresPointX1 = xStart - xEnd

    xStart, xEnd, yStart, yEnd = lightPos2
    if xStart < xEnd:
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd,xStart:xEnd:-1]
    cv.destroyAllWindows()
    cutter_image_show("Ruler2", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate2", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres2", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀1号相机2号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]
    if len(rightContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in rightContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("RightContourJointed", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀1号相机2号灯管终点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        rightContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] > imageRuler.shape[1] / 2]

    if len(rightContours) >= 1:
        finalContour = sorted(rightContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[0], rightClockWisePoint[3]]  # 右边轮廓的左边线
        firstThresPointX2 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint1", imageColor)

    else:
        cv.line(imageColor, (xEnd, yStart), (xEnd, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint", imageColor)
        # 右边的轮廓没有找到,这个时候,要做的事情是什么呢,如果轮廓没有找到,那就证明整个灯管都被搞到了
        if xStart < xEnd:
            firstThresPointX2 = xEnd - xStart
        else:
            firstThresPointX2 = xStart - xEnd
    return int(firstThresPointX1), int(firstThresPointX2), imageColor


def get_first_thres_point_begain_check(image, lightPos1, lightPos2):
    """
    铡刀首检的时候,获取板子到铡刀的距离,这个地方跟终点检测时候很相似,但是这里有一个特点就是找的右边的轮廓,将左边的轮廓改成右边的轮廓就可以了.
    :param image:传入来的处理好的图片,一律按照人看的方式保存图片,人的习惯的方式
    :param lightPos1:1号灯管的位置
    :param lightPos2:2号灯管的位置
    :return:
    """
    xStart, xEnd, yStart, yEnd = lightPos1
    if xStart < xEnd:  # 要看尺寸在图上的选择位置是从前往后,还是从后往前.只有这两种情况.
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd, xStart:xEnd:-1]
    imageColor = cv.cvtColor(image, cv.COLOR_GRAY2BGR)
    cutter_image_show("Ruler1", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate1", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres1", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀1号相机1号灯管起点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    leftContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] <= imageRuler.shape[1] / 2]
    if len(leftContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in leftContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("LeftContourJointed1", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀1号相机1号灯管起点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        leftContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] <= imageRuler.shape[1] / 2]

    if len(leftContours) >= 1:
        finalContour = sorted(leftContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[1], rightClockWisePoint[2]]  # 左轮廓的右边线
        toCutterOffset1 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint1", imageColor)

    else:
        cv.line(imageColor, (xStart, yStart), (xStart, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint1", imageColor)
        toCutterOffset1 = None  # 灯管被完全遮挡了.

    xStart, xEnd, yStart, yEnd = lightPos2
    if xStart < xEnd:
        imageRuler = image[yStart:yEnd, xStart:xEnd]
    else:
        imageRuler = image[yStart:yEnd,xStart:xEnd:-1]
    cv.destroyAllWindows()
    cutter_image_show("Ruler2", imageRuler)
    # 先进行腐蚀膨胀,去除掉噪点
    kernelUsed = (5, 5)
    erodeDilateTimes = 3
    erode = cv.erode(imageRuler, kernelUsed, iterations=erodeDilateTimes)
    dilate = cv.erode(erode, kernelUsed, iterations=erodeDilateTimes)
    cutter_image_show("ErodeDilate2", dilate)
    # 使用的阈值150
    T, rulerThres = cv.threshold(dilate, 150, 255, cv.THRESH_BINARY)
    cutter_image_show("rulerThres2", rulerThres)

    cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
    cntsArea = [cv.contourArea(cnt) for cnt in cnts]
    print("铡刀1号相机2号灯管起点检测查找的轮廓面积:{}".format(cntsArea))

    # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
    leftContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] <= imageRuler.shape[1] / 2]
    if len(leftContours) > 1:
        rightCenters = [get_center_by_contour(contour) for contour in leftContours]
        for index, center in enumerate(rightCenters):
            anotherCenter = rightCenters[(index + 1) % len(rightCenters)]
            cv.line(rulerThres, center, anotherCenter, 255, 20)
        cutter_image_show("RightContourJointed", rulerThres)
        cnts = cv.findContours(rulerThres.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        cnts = [cnt for cnt in cnts if cv.contourArea(cnt) > areaFilter]
        cntsArea = [cv.contourArea(cnt) for cnt in cnts]
        print("铡刀1号相机1号灯管起点检测查找的轮廓面积:{}".format(cntsArea))

        # 然后区分左右两个轮廓,主要是找右边的轮廓,如果是终点检测时候,第一块应该是右边的轮廓.
        leftContours = [cnt for cnt in cnts if cv.minAreaRect(cnt)[0][0] < imageRuler.shape[1] / 2]

    if len(leftContours) >= 1:
        finalContour = sorted(leftContours, key=cv.contourArea, reverse=True)[0]
        boxRectPoints = np.int0(cv.boxPoints(cv.minAreaRect(finalContour)))
        rightClockWisePoint = get_rect_points_clockwise(boxRectPoints)
        rightLine = [rightClockWisePoint[1], rightClockWisePoint[2]]  # 右边轮廓的左边线
        toCutterOffset2 = get_two_points_middle(rightLine[0], rightLine[1])[0]
        # 将坐标定位到大图上去,注意这里的转换关系是什么.它找到的位置是灯管上的位置,如果要换算成大图上去,你想一下如何去换算.
        extend = 10  # 话的线延长10个mm
        if xStart < xEnd:
            newRightLine = [(xStart + rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart + rightLine[1][0], rightLine[1][1] + yStart + extend)]
        else:
            newRightLine = [(xStart - rightLine[0][0], rightLine[0][1] + yStart - extend),
                            (xStart - rightLine[1][0], rightLine[1][1] + yStart + extend)]
        cv.line(imageColor, newRightLine[0], newRightLine[1], (0, 255, 0), 2)
        cutter_image_show("FirstThresPoint1", imageColor)

    else:
        cv.line(imageColor, (xStart, yStart), (xStart, yEnd), (0, 255, 0), 3)
        cutter_image_show("FirstThresPoint", imageColor)
        # 右边的轮廓没有找到,这个时候,要做的事情是什么呢,如果轮廓没有找到,那就证明整个灯管都被搞到了
        toCutterOffset2 = None
    if toCutterOffset1 is None or toCutterOffset2 is None:
        return None, None, imageColor
    return int(toCutterOffset1), int(toCutterOffset2), imageColor


if __name__ == '__main__':
    timeStart = time.time()
    lightPos1Test = [2020, 1, 850, 910]
    lightPos2Test = [2020, 1, 2140, 2200]
    lightPos3Test = [100, 1760, 850, 910]
    lightPos4Test = [100, 1760, 2120, 2180]
    imageTestPath = r"D:\shenghong\cutter_raw\8607--time2021-05-08_01-55-15--end--cam1.bmp"
    imageTestPath2 = r"D:\shenghong\cutter_raw\8602--time2021-05-08_01-53-32--end--cam2.bmp"
    imageTest = cv.imread(imageTestPath, cv.IMREAD_GRAYSCALE)
    imageTest2 = cv.imread(imageTestPath2, cv.IMREAD_GRAYSCALE)
    firstThresPointX1, firstThresPointX2, imageRes1 = get_first_thres_point_final_check_cam_1(imageTest, lightPos1Test, lightPos2Test)
    firstThresPointX3, firstThresPointX4, imageRes2 = get_first_thres_point_final_check_cam_2(imageTest2, lightPos3Test, lightPos4Test)

    print("检测结果,firstThresPointX1 = {},firstThresPointX2 = {},firstThresPointX3 = {},firstThresPointX4 = {} 耗时:{}".format(
        firstThresPointX1, firstThresPointX2, firstThresPointX3, firstThresPointX4, time.time() - timeStart))

    offset1, offset2, imageRes3 = get_first_thres_point_begain_check(imageTest2, lightPos1Test, lightPos2Test)
